﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyFitness.Model;

namespace MyFitness.View
{
    public partial class ExerciseEntryForm : Form
    {
        #region Declared Variables
        private MyFitnessDatabaseEntities database;
        private Exercise exercise;
        #endregion

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public ExerciseEntryForm(MyFitnessDatabaseEntities db)
        {
            exercise = new Exercise();

            Initialize(db);
        }

        /// <summary>
        /// Constructor to load the form with data.
        /// </summary>
        /// <param name="e"></param>
        public ExerciseEntryForm(MyFitnessDatabaseEntities db, Exercise e)
        {
            exercise = e;

            Initialize(db);
        }

        /// <summary>
        /// Initializes information for either constructor.
        /// </summary>
        private void Initialize(MyFitnessDatabaseEntities db)
        {
            InitializeComponent();
            CenterToParent();

            // Connects to the database to pull exercise types
            database = db;

            // Create handlers for tracking when an Exercise Entry is validated for saving an entry.
            ActivityComboBox.SelectedValueChanged += new EventHandler(OnEnableOKButton);
            DurationDateTimePicker.ValueChanged += new EventHandler(OnEnableOKButton);
            CalorieBurnTextBox.TextChanged += new EventHandler(OnEnableOKButton);
        }

        /// <summary>
        /// Gets the Exercise Object from the information entered into the dialog.
        /// </summary>
        /// <returns>Returns an Exercise Object</returns>
        public Exercise GetExerciseEntry()
        {
            try
            {
                // Gets the exercise type from the ActivityComboBox.
                ComboboxItem c = (ComboboxItem)ActivityComboBox.SelectedItem;

                if (c == null)
                    return null;

                ExerciseType et = (ExerciseType)c.Value;

                if (et == null)
                    return null;

                // Sets the exercise type ID to correlate to the exercise information
                exercise.ExerciseTypeID = et.Id;

                DateTime d = DurationDateTimePicker.Value;
                exercise.Duration = new TimeSpan(d.Hour, d.Minute, d.Second);

                if (CalorieBurnTextBox.Text.Length > 0)
                    exercise.CaloriesBurned = Convert.ToDouble(CalorieBurnTextBox.Text);

                return exercise;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Load Form Event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExerciseEntryForm_Load(object sender, EventArgs e)
        {
            var exercisetypes = database.ExerciseTypes;

            // Adds exercises to the ActivityComboBox.
            foreach (var ex in exercisetypes)
            {
                ComboboxItem c = new ComboboxItem(ex.Name, ex);
                ActivityComboBox.Items.Add(c);

                // Selects the current exercise from the exercise object.
                if( exercise.ExerciseTypeID == ex.Id )
                    ActivityComboBox.SelectedItem = c;
            }

            // Loads the calorie values from the exercise object
            CalorieBurnTextBox.Text =  exercise.CaloriesBurned.ToString();

            // Setting the time value of the DurationDateTime Picker.
            DateTime time = new DateTime(2013, 1, 1, exercise.Duration.Hours, exercise.Duration.Minutes, exercise.Duration.Seconds);
            DurationDateTimePicker.Value = time;
        }

        /// <summary>
        /// Enables or Disables the OKButton depending on validated input.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnEnableOKButton(object sender, EventArgs e)
        {
            // Enables the "OK" Button if all the fields are validated, i.e. has a first and last name...
            if (ActivityComboBox.SelectedItem != null)
                okButton.Enabled = true;
            else
                okButton.Enabled = false;
        }
    }
}
